<?php

class paomeClubController extends base
{
    public function __construct()
    {
        parent::__construct('admin');
        // 判断权限
        if(empty($this->realUser['uid']) || !$this->load('adminList')->isAdminByHupuUid($this->realUser['uid']))
        {
            echo 'jrs, 不要四处乱看哦～～';
            exit();
        }
    }

    public function doinit() {}

    /**
     * 俱乐部列表
     */
    public function dogetClubList() {
        $paomeId =  common::request('paomeId','R',0);//赛季ID
        if (!$paomeId) {
            common::tips('参数丢失');exit;
        }
        $arrInfo = $this->load('paome/cup')->getPaomeInfo($paomeId);
        if (!$arrInfo) {
            common::tips('赛事不存在');exit;
        }
        $arrType = array(//页面展示
            1=>'第一轮预选赛',
            5=>'第一轮决赛',
            10=>'第二轮预选赛',
            15=>'第二轮决赛',
//            20=>'第三轮预选赛',
//            25=>'第三轮决赛',
            100=>'争霸赛'
        );
        $arrTypeStatus = array(
            0=>'招募中',
            5=>'淘汰：人数未满下限',
            10=>'淘汰：未能晋级冠军赛',
            15=>'竞赛中',
            20=>'已结束'
        );
        $arrStatus = array(//page 选择
            array('name'=>'第一轮','child'=>array(array('id'=>1,'name'=>'预赛'),array('id'=>5,'name'=>'冠军赛'))),
            array('name'=>'第二轮','child'=>array(array('id'=>10,'name'=>'预赛'),array('id'=>15,'name'=>'冠军赛'))),
            //array('name'=>'第三轮','child'=>array(array('id'=>20,'name'=>'预赛'),array('id'=>25,'name'=>'冠军赛'))),
            array('name'=>'冠军争霸赛','child'=>array(array('id'=>100,'name'=>'争霸赛')))
        );

        $name   = common::request('name','R','');//名称
        $status = common::request('status','R',0);//俱乐部状态
        $contestStatus = common::request('contestStatus','R',0);//赛季轮次状态
        $contestStatus2 = common::request('contestStatus2','R',0);//竞赛状态
        $contestStatus3 = common::request('contestStatus3','R',0);//赛绩状态
        $userNumType = common::request('userNumType','R',0);//人数排序
        $listNum = 20;
        $p = common::request('p', 'G', 1);
        $start = ($p - 1) * $listNum;
        $parameter = '';
        $params = [];
        $params['paomeId'] = $paomeId;
        if (trim($name)) {
            $parameter .= '&name='."$name";
            $params['name'] = $name;
        }
        if ($status) {
            $parameter .= '&status='."$status";
            $params['status'] = $status;
        }
        if ($contestStatus) {
            $parameter .= '&contestStatus='."$contestStatus";
            $params['contestStatus'] = $contestStatus;
        }
        if ($contestStatus2) {
            $parameter .= '&contestStatus2='."$contestStatus2";
            $params['contestStatus2'] = $contestStatus2;
        }
        if ($contestStatus3) {
            $parameter .= '&contestStatus3='."$contestStatus3";
            $params['contestStatus3'] = $contestStatus3;
        }
        if ($userNumType) {
            $parameter .= '&userNumType='."$userNumType";
            $params['userNumType'] = $userNumType;
        }

        $params['start'] = $start;
        $params['limit'] = $listNum;
        $list = $this->load('paome/paomeClub')->getClubList($params);
        if($list) {
            $clubStatus = array(0=>'正常',-5=>'解散');
            foreach ($list as $key=>$val) {
                $list[$key]['formatTime'] = $val['create_time'] ? date('Y-m-d H:i:s',$val['create_time']) : '';
                $list[$key]['statusMsg'] = isset($clubStatus[$val['status']]) ? $clubStatus[$val['status']] : '未知状态';
                $list[$key]['typeMsg'] = isset($arrType[$val['bType']]) ? $arrType[$val['bType']] : '---';//截止赛事阶段
                $list[$key]['typeStatusMsg'] = isset($arrTypeStatus[$val['bStatus']]) ? $arrTypeStatus[$val['bStatus']] : '---';//赛事阶段状态
            }
        }
        $total = $this->load('paome/paomeClub')->total;
        require FILE . 'lib/page.class.php';
        $page = new Page($total, $listNum, $parameter);
        $pageStr = $page->show();

        $this->view->assign('list', $list);
        $this->view->assign('pageStr', $pageStr);
        $this->view->assign('name', $name);
        $this->view->assign('status', $status);
        $this->view->assign('contestStatus', $contestStatus);
        $this->view->assign('contestStatus2', $contestStatus2);
        $this->view->assign('contestStatus3', $contestStatus3);
        $this->view->assign('userNumType', $userNumType);
        $this->view->assign('arrStatus', $arrStatus);
        $this->view->assign('paomeId', $paomeId);
        $this->view->display('paome/clubList');
    }

    /**
     * 俱乐部详情
     */
    public function dogetClubDetail() {
        $paomeId =  common::request('paomeId','R',0);//赛季ID
        $clubId = common::request('clubId','R',0);//俱乐部ID

        $info = $this->load('paome/paomeClub')->getClubDetail($paomeId,$clubId);
        if($info) {
            $arrStatusMsg = common::$arrStatusMsg;
            $arrTypeStatus = array(
                0=>'招募中',
                5=>'淘汰：人数未满下限',
                10=>'淘汰：未能晋级冠军赛',
                15=>'竞赛中',
                20=>'已结束'
            );
            $clubStatus = array(0=>'正常',-5=>'解散');
            $info['formatTime'] = $info['create_time'] ? date('Y-m-d H:i:s',$info['create_time']) : '';
            $info['statusMsg'] = isset($clubStatus[$info['status']]) ? $clubStatus[$info['status']] : '未知状态';
            $info['typeMsg'] = isset($arrStatusMsg[$info['bType']]) ? $arrStatusMsg[$info['bType']] : '---';//截止赛事阶段
            $info['typeStatusMsg'] = isset($arrTypeStatus[$info['bStatus']]) ? $arrTypeStatus[$info['bStatus']] : '---';//赛事阶段状态
        }
//        print_r($info);
        //俱乐部当前成员
        $arrMemeber = $this->db->getAll("select * from r_pm_club_users where club_id=$clubId and status in(-1,0) order by level desc,create_time desc");
        if ($arrMemeber) {
           foreach($arrMemeber as $key=>$val) {
               $header = $this->redis->hget(REDIS_PREFIX . 'UserInfo_' . $val['uid'],'header');
               $nickname = $this->redis->hget(REDIS_PREFIX . 'UserInfo_' . $val['uid'],'nickname');
               $arrMemeber[$key]['header'] = $header;
               $arrMemeber[$key]['nickname'] = $nickname;
               $arrMemeber[$key]['statusMsg'] = $val['status'] == -1 ? '待审核' : '已加入';
               $arrMemeber[$key]['levelMsg'] = $val['level'] == 5 ? '团长' : '成员';
               $arrMemeber[$key]['formatTime'] = $val['create_time'] ? date('Y-m-d H:i:s',$val['create_time']) : '';
           }
        }

        //赛季记录
        $arrInfo = $this->load('paome/cup')->getPaomeInfo($paomeId);
        $arrGameLog = [];
        if ($arrInfo) {
            $arrGameLog =  $this->load('paome/paomeClub')->getClubGameLog($paomeId, $arrInfo['status'], $clubId);
        }

        $this->view->assign('arrMemeber', $arrMemeber);
        $this->view->assign('info', $info);
        $this->view->assign('arrGameLog', $arrGameLog);
        $this->view->assign('paomeId', $paomeId);
        $this->view->display('paome/clubDetail');
    }

    /**
     * 俱乐部 人员列表
     */
    public function dogetUserList() {
        $paomeId =  common::request('paomeId','R',0);//赛季ID
        if (!$paomeId) {
            common::tips('参数丢失');exit;
        }
        $arrInfo = $this->load('paome/cup')->getPaomeInfo($paomeId);
        if (!$arrInfo) {
            common::tips('赛事不存在');exit;
        }
        $arrStatus = array(//page 选择
            array('name'=>'第一轮','child'=>array(array('id'=>1,'name'=>'预赛'),array('id'=>5,'name'=>'冠军赛'))),
            array('name'=>'第二轮','child'=>array(array('id'=>10,'name'=>'预赛'),array('id'=>15,'name'=>'冠军赛'))),
            //array('name'=>'第三轮','child'=>array(array('id'=>20,'name'=>'预赛'),array('id'=>25,'name'=>'冠军赛'))),
            array('name'=>'冠军争霸赛','child'=>array(array('id'=>100,'name'=>'争霸赛')))
        );

        $name   = common::request('name','R','');//人员名称
        $clubName   = common::request('clubName','R','');//俱乐部名称
        $status = common::request('status','R',0);//成员状态
        $contestStatus = common::request('contestStatus','R',0);//赛季轮次状态
        $contestStatus2 = common::request('contestStatus2','R',0);//俱乐部竞赛状态
        $contestStatus3 = common::request('contestStatus3','R',0);//赛绩排序
        $addTimeType = common::request('addTimeType','R',0);//加入时间排序
        $listNum = 20;
        $p = common::request('p', 'G', 1);
        $start = ($p - 1) * $listNum;
        $parameter = '';
        $params = [];
        $params['paomeId'] = $paomeId;
        if (trim($name)) {
            $parameter .= '&name='."$clubName";
            $params['name'] = $name;
        }
        if (trim($clubName)) {
            $parameter .= '&clubName='."$clubName";
            $params['clubName'] = $clubName;
        }
        if ($status) {
            $parameter .= '&status='."$status";
            $params['status'] = $status;
        }
        if ($contestStatus) {
            $parameter .= '&contestStatus='."$contestStatus";
            $params['contestStatus'] = $contestStatus;
        }
        if ($contestStatus2) {
            $parameter .= '&contestStatus2='."$contestStatus2";
            $params['contestStatus2'] = $contestStatus2;
        }
        if ($contestStatus3) {
            $parameter .= '&contestStatus3='."$contestStatus3";
            $params['contestStatus3'] = $contestStatus3;
        }
        if ($addTimeType) {
            $parameter .= '&addTimeType='."$addTimeType";
            $params['addTimeType'] = $addTimeType;
        }

        $params['start'] = $start;
        $params['limit'] = $listNum;
        $list = $this->load('paome/paomeClub')->getUserList($params);
        if($list) {
            $userStatus = array(0=>'正常',-1=>'待审核', -5=>'已退出');
            foreach ($list as $key=>$val) {
                $list[$key]['formatTime'] = $val['create_time'] ? date('Y-m-d H:i:s',$val['create_time']) : '';
                $list[$key]['statusMsg'] = isset($userStatus[$val['status']]) ? $userStatus[$val['status']] : '未知状态';
                //用户信息
                $header = $this->redis->hget(REDIS_PREFIX . 'UserInfo_' . $val['uid'],'header');
                $nickname = $this->redis->hget(REDIS_PREFIX . 'UserInfo_' . $val['uid'],'nickname');
                $list[$key]['userHeader'] = $header;
                $list[$key]['userNickName'] = $nickname;
                //所属俱乐部
                $arrClubInfo = $this->load('paome/paomeClub')->getClubInfo($val['club_id']);
                $list[$key]['clubInfo'] = $arrClubInfo;
                //战队当前所出的比赛阶段
                $arrRound2 = $this->db->getRow("select `type`,status from r_pm_club_status where contest_id=$paomeId and club_id={$val['club_id']}");
                $arrStatusMsg = common::$arrStatusMsg;
                $list[$key]['typeMsg'] = isset($arrStatusMsg[$arrRound2['type']]) ? $arrStatusMsg[$arrRound2['type']] : '';
                //个人数据
                $list[$key]['bmileage'] = 0;//累计跑量
                $list[$key]['bMaxMileage'] = 0;//最远跑量
                $list[$key]['bSpeed'] = 0;   //平均配速
                $list[$key]['bSupportNum'] = 0;//累计人气
                if (isset($arrStatusMsg[$arrRound2['type']])) {
                    $arrUserRunData = $this->db->getRow("select mileage,speed,support_num,max_mileage from r_pm_users_status where contest_id=$paomeId and club_id={$val['club_id']} and uid={$val['uid']} and `type`={$arrRound2['type']} ");
                    if (!$arrUserRunData) continue;
                    $list[$key]['bmileage'] = $arrUserRunData['mileage'];//累计跑量
                    $list[$key]['bMaxMileage'] = $arrUserRunData['max_mileage'];//最远跑量
                    $list[$key]['bSpeed'] = $arrUserRunData['speed'] > 0 ? common::sec3format($arrUserRunData['speed']) : 0;  //平均配速
                    $list[$key]['bSupportNum'] = $arrUserRunData['support_num']; //累计人气
                }
            }
        }
        $total = $this->load('paome/paomeClub')->total;
        require FILE . 'lib/page.class.php';
        $page = new Page($total, $listNum, $parameter);
        $pageStr = $page->show();

        $this->view->assign('list', $list);
        $this->view->assign('pageStr', $pageStr);
        $this->view->assign('name', $name);
        $this->view->assign('clubName', $clubName);
        $this->view->assign('status', $status);
        $this->view->assign('contestStatus', $contestStatus);//赛季阶段
        $this->view->assign('contestStatus2', $contestStatus2);//赛事状态
        $this->view->assign('contestStatus3', $contestStatus3);//赛绩排序
        $this->view->assign('addTimeType', $addTimeType);
        $this->view->assign('arrStatus', $arrStatus);
        $this->view->assign('paomeId', $paomeId);
        $this->view->display('paome/userList');
    }

    /**
     * 俱乐部 人员详情
     */
    public function dogetUserDetail() {
        $paomeId =  common::request('paomeId','R',0);//赛季ID
        $clubId = common::request('clubId','R',0);//俱乐部ID
        $uid = common::request('uid','R',0);

        if (!$paomeId || !$clubId || !$uid) {
            common::tips('参数丢失');exit;
        }
        $header = $this->redis->hget(REDIS_PREFIX . 'UserInfo_' . $uid,'header');
        $nickname = $this->redis->hget(REDIS_PREFIX . 'UserInfo_' . $uid,'nickname');
//        $userInfo = $this->redis->hGetAll(REDIS_PREFIX . 'UserInfo_' . $uid);
//        if ($userInfo) {
//            common::tips('用户查询失败');exit;
//        }
        $arrInfo = $this->load('paome/cup')->getPaomeInfo($paomeId);
        if (!$arrInfo) {
            common::tips('赛事不存在');exit;
        }

        $info = $this->load('paome/paomeClub')->getUserDetail($paomeId,$clubId,$uid);
        if($info) {
            $userStatus = array(0=>'正常',-1=>'待审核',-5=>'退出');
            $info['formatTime'] = $info['create_time'] ? date('Y-m-d H:i:s',$info['create_time']) : '';
            $info['statusMsg'] = isset($userStatus[$info['status']]) ? $userStatus[$info['status']] : '未知状态';
            //战队当前所出的比赛阶段
            $arrRound2 = $this->db->getRow("select `type`,status from r_pm_club_status where contest_id=$paomeId and club_id=$clubId");
            $arrStatusMsg = common::$arrStatusMsg;
            $info['typeMsg'] = isset($arrStatusMsg[$arrRound2['type']]) ? $arrStatusMsg[$arrRound2['type']] : '';
            //个人数据
            $info['bmileage'] = 0;//累计跑量
            $info['bMaxMileage'] = 0;//最远跑量
            $info['bSpeed'] = 0;   //平均配速
            $info['bSupportNum'] = 0;//累计人气
            $info['bAgreeNum'] = 0; //累计点赞
            $info['bEnergyNum'] = 0; //累计正能量
            if (isset($arrStatusMsg[$arrRound2['type']])) {
                $arrUserRunData = $this->db->getRow("select mileage,speed,support_num,max_mileage,agree_num,energy_num from r_pm_users_status where contest_id=$paomeId and club_id=$clubId and uid={$info['uid']} and `type`={$arrRound2['type']} ");
                if ($arrUserRunData) {
                    $info['bmileage'] = $arrUserRunData['mileage'];//累计跑量
                    $info['bMaxMileage'] = $arrUserRunData['max_mileage'];//最远跑量
                    $info['bSpeed'] = $arrUserRunData['speed'] > 0 ? common::sec3format($arrUserRunData['speed']) : 0;  //平均配速
                    $info['bSupportNum'] = $arrUserRunData['support_num']; //累计人气
                    $info['bAgreeNum'] = $arrUserRunData['agree_num']; //累计点赞
                    $info['bEnergyNum'] = $arrUserRunData['energy_num']; //累计正能量
                }
            }
        }
        $info['userNickName'] = $nickname;
        $info['userHeader'] = $header;
        $arrClubInfo = $this->load('paome/paomeClub')->getClubInfo($clubId,'title');
        $info['clubName'] = $arrClubInfo['title'];
//        print_r($info);
        $this->view->assign('info', $info);
        $this->view->assign('paomeId', $paomeId);
        $this->view->display('paome/userDetail');
    }

    /**
     * 上传俱乐部头像
     */
    public function doajaxUploadImage() {
        if (!$_FILES || ($_FILES['imgFile']['size'] == 0)) {
            echo json_encode(array('status'=>'fail','error_msg'=>'请选择上传文件!'));
            exit;
        }
        $imageName ='paomeCup_Club_thumb_'.md5(microtime(TRUE).$_FILES['imgFile']['name']);
        $image_url = $this->load('qiniu')->uploadQiNiuImg('imgFile',$imageName);
        if (!$image_url) {
            echo json_encode(array('status'=>'fail','error_msg'=>'上传失败'));exit;
        }
        echo json_encode(array('status'=>'ok','error_msg'=>'上传成功','image'=>$image_url));
    }

    /**
     * 后台把待审核自动加入战队
     */
    public function dojoinAgreeClub() {
        $this->printResult = true;
        $uid = (int)common::request('uid', 'P', 0, true);
        $clubId = (int)common::request('clubId','P',0,true);
        $this->printResult = TRUE;
        /*申请加入俱乐部start*/
        if (!$uid || !$clubId) {
            $_ENV['error']->getErrors(101);exit;
        }
        $status = $this->load('paome/paomeClub')->joinClub($uid, $clubId);
        /*申请加入俱乐部end*/
        if ($status<0) {
            $errorMap = array('-1'=>6012,'-2'=>6013,'-3'=>6014, '-4'=>6015, '-5'=>6016, '-6'=>6017, '-7'=>6018);//错误码映射
            $_ENV['error']->getErrors($errorMap[$status]);
            exit;
        }
        /*团长同意加入俱乐部start*/
            //战队info
            $arrClub = $this->load('paome/paomeClub')->getClubInfo($clubId,'id,head_uid');
            if (!$arrClub) {
                $_ENV['error']->getErrors(6012);exit;
            }
            $headUid = $arrClub['head_uid'];//团长
            $mid = $this->db->getOne("select id from r_pm_message where from_uid =$uid  and to_uid =$headUid and desc_type='P1'");
            if (!$mid) {
                $_ENV['error']->getErrors(101);exit;
            }
            //同意加入
            $status = $this->load('paome/paomeClub')->agreeJoin($headUid, $mid);
            if ($status<0) {
                $errorMap = array('-1'=>6016,'-2'=>6019,'-3'=>6026,
                    '-4'=>6012,'-5'=>6013,'-6'=>6017,
                    '-7'=>6022,'-8'=>6014,'-9'=>6018,
                    '-10'=>6021,'-11'=>6026);//错误码映射
                $_ENV['error']->getErrors($errorMap[$status]);
                exit;
            }
        /*团长同意加入俱乐部start*/
        $this->result['result']['is_success'] = 1;
    }
}